package com.lms.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.neo4j.conversion.EndResult;
import org.springframework.stereotype.Service;

import com.lms.domain.NewProjects;
import com.lms.repository.NewProjectRepository;

@Service
public class ProjectService {
	
	
	
	@Autowired
	private NewProjectRepository projectRepository;
	
	
	public NewProjects create1(NewProjects project) {
		NewProjects existingProject = projectRepository.findByProjectId(project.getProjectId());
		System.out.println("\n\n\n\n\n 111111122222211111122222222 \n\n\n\n\n\n");
		if (existingProject != null) {
			throw new RuntimeException("Record already exists!");
		}
		
		//project.getRole().setUser(user);
		return projectRepository.save(project);
	}

	public NewProjects read1(NewProjects project) {
		return project;
	}
	
	public List<NewProjects> readAll1() {
		List<NewProjects> projects = new ArrayList<NewProjects>();
		
		EndResult<NewProjects> results = projectRepository.findAll();
		for (NewProjects rs: results) {
			projects.add(rs);
		}
		
		return projects;
	}
	
	public NewProjects update1(NewProjects project) {
		NewProjects existingProject = projectRepository.findByProjectId(project.getProjectId());
		
		if (existingProject == null) {
			return null;
		}
		
		existingProject.setProjectId(project.getProjectId());
		existingProject.setProjectName(project.getProjectName());
		existingProject.setProjectStartDate(project.getProjectStartDate());
		existingProject.setProjectEndDate(project.getProjectEndDate());
		
		//projectRepository.save(existingProject.getRole());
		return projectRepository.save(existingProject);
	}
	
	public NewProjects readProjectSearch(NewProjects project) {
		NewProjects existingProject = projectRepository.findByProjectId(project.getProjectId());
		
		
		System.out.println(project.getProjectId());
		if (existingProject != null) {
			System.out.println("Search result..."+existingProject.getProjectName());
			return existingProject;
		}
		else
			System.out.println("No Such Project");
		
		return null;
		
		
	}
}
